package com.csw.shuanfa.utils.dateTime;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 节假日获取
 */

/**
 * 效果实例
 * 2023-1-1节假日
 * 2023-1-2周末
 * 2023-1-3上班
 * 非工作时间段内
 * 2023-1-4上班
 * 非工作时间段内
 * 2023-1-5上班
 * 非工作时间段内
 * 2023-1-6上班
 * 非工作时间段内
 * 2023-1-7周末
 * 2023-1-8周末
 * 2023-1-9上班
 */
public class HolidayUtil {
    private static final String ZERO_0 = "0";
    private static final String ZERO_1 = "1";
    private static final String ZERO_2 = "2";

    public static void main(String[] args) {
        List woringDays = judgeDate();

//是工作日

    }

    /**
     * 判断今日是否为工作日
     *
     * @param httpArg
     * @return
     */
    public static String request(String httpArg) {
        String httpUrl = "http://tool.bitefu.net/jiari/";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List judgeDate() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(calendar.YEAR), 01, 01);
        int year = calendar.get(calendar.YEAR);

        List list = new ArrayList();
        while (calendar.get(Calendar.YEAR) == year) {
            String month = calendar.get(Calendar.MONTH) + "";
            String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
            String nyr = year + "/" + month + "/" + day;
            if (month.length() == 1) {
                month = "0" + month;
            }
            if (day.length() == 1) {
                day = "0" + day;
            }
//            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
//            //获取当前时间，格式为yyyyMMdd
//            String woringDays = sf.format(new Date());
            String woringDays = year + month + day + "";
            String jsonResult = HolidayUtil.request(woringDays);


            // 增加一天
            calendar.add(Calendar.DAY_OF_MONTH, 1);

            // 0 上班  1周末 2节假日
//            if (ZERO_0.equals(jsonResult)) {
//                woringDays = ZERO_0;
//            }
//            if (ZERO_1.equals(jsonResult)) {
//                woringDays = ZERO_1;
//
//            }
//            if (ZERO_2.equals(jsonResult)) {
//                woringDays = ZERO_2;
//            }
            if (ZERO_0.equals(jsonResult)) {
                System.out.println(nyr + "上班");
                //工作日");
/**
 * 判断当前时间是否在一个时间段内 HH:mm 格式
 */
                String strStartTime1 = "00:00";
                String strEndTime1 = "6:00";

                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String now = sdf.format(new Date());
                Date nowTime;
                try {
                    nowTime = sdf.parse(now);
                    Date startTime1 = sdf.parse(strStartTime1);
                    Date endTime1 = sdf.parse(strEndTime1);
                    // 注：也可以通过此方式判断当前时间是否具体时间段内 yyyy-MM-dd HH:mm:ss格式 [2022-03-09 12:00:00,2022-03-10 15:00:00]
                    //   当前时间和时间段的格式保持一致即可判断
                    if (isEffectiveDate(nowTime, startTime1, endTime1)) {
                        System.out.println("在工作时间段内");
                        //工作时间段");
                    } else {//非工作时间段
                        //非工作时间段");
                        System.out.println("非工作时间段内");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            if (ZERO_1.equals(jsonResult)) {
                woringDays = ZERO_1;
                System.out.println(nyr + "周末");
                list.add(nyr);
            }
            if (ZERO_2.equals(jsonResult)) {
                woringDays = ZERO_2;
                System.out.println(nyr + "节假日");
                list.add(nyr);
            }
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        return list;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意三个参数的时间格式要一致
     *
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return 在时间段内返回true，不在返回false
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }
}
